import setting from "./setting";
import { doRequest,setRouter } from "./common";
const api_file = "api.html";
var isSetRouter = 0;
const ajax = {
	return_all_data:0,
	request(method,path_url,url,data){
		if (!data['headers']) {
			data['headers'] = {}
		}
		var loginX = localStorage.getItem('uniqidKey') || "";
		data['headers']['servertoken'] = loginX;
		data['headers']['server'] = url;
		// data['headers']['sign'] = md5('123');
		data['baseURL'] = setting.API_HOST;
		data['method'] = method;
		data['data'] = JSON.stringify(data['data']);
		return (doRequest(path_url, data,this.return_all_data));
	},

	post(url,data){
		data = data||{};
		let post_data={};
		post_data['headers'] = {}
		post_data['data'] = data;
		return this.request('post',api_file,url,post_data);
	},
	upfile(url,data){
		const from_data = new FormData();
		if(!data.name){
			if(data.file){
				var file = data.file;
				if(data.data){
					let form_data = data.data;
					for(let key in form_data ){
						from_data.append(key,form_data[key]);
					}
				}
			}else{
				return console.log('没有选择上传文件');
			}
			if(!data.file.name) return console.log('没有选择上传文件');
		}else{
			var file = data;
		}
		from_data.append("file1",file);
		var post_data={};
		post_data['headers'] = {};
		var loginX = localStorage.getItem('uniqidKey') || "";
		post_data['headers']['servertoken'] = loginX;
		post_data['headers']['Content-Type'] = 'multipart/form-data';
		post_data['headers']['server'] = url;
		post_data['baseURL'] = setting.API_HOST;
		post_data['method'] = "post";
		post_data['data'] = from_data
		return (doRequest(api_file,post_data,this.return_all_data));
	}
}
var setStorageData = function(res){
	localStorage.setItem("uniqidKey",res.key);//设置本地存储
	localStorage.setItem("this_uid",res.uid);
	localStorage.setItem("userInfo",JSON.stringify(res.userInfo));
}
var query = function(data, key) {
	key = key ? key : "";
	var query = [];
	for (let item_key in data) {
		if (key != item_key) {
			var str = item_key + "=" + data[item_key];
			query.push(str);
		}
	}
	return query;
}
var $setRouter = function(router){
	if(isSetRouter) return;
	try {
		setRouter(router);
		isSetRouter = 1;
	} catch (error) {
		console.log('run:npm install --save wq_api', error);
	}
}
//获取该方法之后请判断是否字符串 成功 是数组
var upfile_success=res=>{
	if(res.state==1){
		return res.data;//数组
	}else{
		return res.msg;
	}
}
var setPath = function(src) {
	if (!src) return;
	if (src.indexOf("http") == 0) return src;
	return location.origin + src;
}
var nl2br = function(str,is_xhtml) {
	var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
var check_auth = res=>{
	if(res=="no") return true;
	let userRoleMenu = localStorage.getItem("userRoleMenu");
	return (userRoleMenu.indexOf(location.pathname)>0)?true:false;
}
let upload_config =function(){
	let data =  {url:setting.API_HOST+api_file,headers:{"SERVERTOKEN":(localStorage.getItem('uniqidKey') || ""),"server":"upfile.oss"},success:upfile_success}
	return data;
}
export default{query,setPath,nl2br,setStorageData,ajax,upload_config,setRouter:$setRouter,check_auth}